package com.tj.jinzao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseListAdapter;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.FamilyMember;
import com.tj.jinzao.widgets.RoundedImageView;

import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 家庭成员 Adapter
 * Created by wjy on 2019/1/10.
 */
public class FamilyListItemAdapter extends BaseListAdapter<FamilyMember, FamilyListItemAdapter.ViewHolder> {

    public FamilyListItemAdapter(Context context, CopyOnWriteArrayList data) {
        super(context, data);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_member_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        FamilyMember familyMember = mData.get(position);
        holder.name.setText(familyMember.getName());
        RequestOptions options = new RequestOptions();
        options.placeholder(familyMember.getGender() == 1
                ? R.mipmap.default_head_women : R.mipmap.default_head_man);
        Glide.with(mContext).load(Constant.IMAGE_PREFIX + familyMember.getImgurl()).apply(options).into(holder.head);
        holder.content_lay.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(onLongLinstener!=null){
                    onLongLinstener.onLongClick(familyMember.getId());
                }
                return false;
            }
        });
    }
    public interface OnLongLinstener{
        void onLongClick(int id);
    }
    public OnLongLinstener onLongLinstener;
    public void setOnLongLinstener(OnLongLinstener onLongLinstener){
        this.onLongLinstener=onLongLinstener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        RoundedImageView head;
        RelativeLayout content_lay;

        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.tv_name);
            head = itemView.findViewById(R.id.iv_head);
            content_lay = itemView.findViewById(R.id.content_lay);
        }
    }


}
